package de.tu_darmstadt.gdi1.battleship.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Event {
	private String name;
	private int turnNumber;
	private int turnTime;
	private Coordinates coords;
	private long startTime;
	private Date date;

	public Event(String name, int turnNumber, Coordinates coords, int turnTime,
			long startTime) {
		super();
		this.name = name;
		this.turnNumber = turnNumber;
		this.coords = coords;
		this.turnTime = turnTime;
		this.startTime = startTime;
		this.date = new Date(startTime);
	}

	// fertiger Highscore Eintrag
	public Event(String name, int turnNumber, double turnTime, Date date) {
		super();
		this.name = name;
		this.turnNumber = turnNumber;
		this.turnTime = (int) turnTime;
		this.date = date;
		this.startTime = date.getTime() - (int)(turnTime*1000);
	}
	
	public long getStartTime() {
		return startTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getTurnNumber() {
		return turnNumber;
	}

	public void setTurnNumber(int turnNumber) {
		this.turnNumber = turnNumber;
	}

	public float getTurnTime() {
		return turnTime;
	}

	public void setTurnTime(int turnTime) {
		this.turnTime = turnTime;
	}

	public Coordinates getCoords() {
		return coords;
	}

	public void setCoords(Coordinates coords) {
		this.coords = coords;
	}
/**
 * String method for an the output of an event
 * @return
 * Event - String
 */
	public String toStringForEventList() {
		StringBuffer sb = new StringBuffer();
		sb.append("   ").append(getTurnNumber()).append(";   ").append(
				getName()).append(";   ").append(getTurnTime()).append(";  \n").append(getCoords().getX()).append(" : ").append(getCoords().getY());
		return sb.toString();
	}
/**
 * generates String for the Highscorelist
 * @return
 * highscore event string
 */
	public String toStringForHighscoreList() {// datum muss

		// TODO: delete Comment
		// Einige Funktionen habe ich ausgelagert damit ich sie im Highscore
		// View verwenden kann
		// /Yannic

		StringBuffer sb = new StringBuffer();
		sb.append("   ").append(getTurnNumber()).append(";            ")
				.append(getDurationString()).append(";        ")
				.append(getDateString()).append(";   ").append(getName())
				.append(";  \n");
		return sb.toString();
	}

	public String getDurationString() {
		int turnMinutes = (int) Math.floor(getTurnTime() / 60);
		int turnSeconds = (int) (getTurnTime() - (60 * turnMinutes));

		String timeString;
		if (turnMinutes < 10) {
			if (turnSeconds < 10) {
				timeString = "0" + turnMinutes + ":" + "0" + turnSeconds;
			} else {
				timeString = "0" + turnMinutes + ":" + turnSeconds;
			}
		} else {
			if (turnSeconds < 10) {
				timeString = turnMinutes + ":" + "0" + turnSeconds;
			} else {
				timeString = turnMinutes + ":" + turnSeconds;
			}
		}

		return timeString;
	}

	public String getDateString() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(getStartTime());

		DateFormat dateformatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

		return dateformatter.format(cal.getTime());
	}
/**
 * generates save string
 * @return
 * save string
 */
	public String generateSaveString() {
		StringBuffer sb = new StringBuffer("#EV\n");

		sb.append(name).append('\n');
		sb.append(turnNumber).append('\n');
		sb.append(turnTime).append('\n');
		sb.append(startTime).append('\n');
		sb.append(coords.generateSaveString());

		return sb.toString();
	}
	/**
	 * reads an event save string
	 * @param br
	 * BufferedReader
	 * @throws IOException
	 */
	public void loadSaveString(BufferedReader br) throws IOException {

		if (br.readLine().equals("#EV")) {
			name = br.readLine();
			turnNumber = Integer.valueOf(br.readLine());
			turnTime = Integer.valueOf(br.readLine());
			startTime = Long.valueOf(br.readLine());
			coords.loadSaveString(br);
			date = new Date(startTime);
		} else {
			System.err.println("Buffered Reader does not begin with #EV");
		}
	}

	public Date getDate() {
		return date;
	}
}
